package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.Result2;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealPojo;
import com.itheima.pojo.dto.SetmealDto;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.Configuration;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    private Configuration configuration;//

    /**
     * 管理端添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping("/add")
    public Result2 add(@RequestBody SetmealDto setmealDto , String checkgroupIds){
        log.info("添加套餐：{}",setmealDto);
        setmealService.add(setmealDto,checkgroupIds);
        return new Result2(true,"添加成功");
    }
    /**
     * 管理端删除套餐
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result2 delete(Integer id){
        log.info("删除套餐：{}",id);
        setmealService.deleteById(id);
        return new Result2(true,"删除成功");
    }
    /**
     * 管理端修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping("/update")
    public Result2 update(@RequestBody SetmealDto setmealDto,String checkgroups){
        log.info("修改套餐：{}",setmealDto);
        setmealService.update(setmealDto,checkgroups);
        return new Result2(true,"修改成功");
    }
    /**
     * 管理端分页查询套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询：{}",queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }
    /**
     * 管理端查询所有套餐
     * @return
     */
    @GetMapping("/findAll")
    public Result2 findAll(){
        log.info("查询所有套餐");
        List<SetmealPojo> list = setmealService.findAll();
        return new Result2(true,"查询成功",list);
    }
    /**
     * 管理端根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result2 getById(Integer id){
        log.info("查询套餐：{}",id);
        SetmealPojo setmeal = setmealService.getById(id);
        return new Result2(true,"查询成功",setmeal);
    }
   /* *//**
     * 修改套餐列表页面为FreeMarker模板
     *//*
    @GetMapping("/findById")
    public String getById(Integer id, Model mod){

        log.info("查询套餐：{}",id);
        SetmealPojo setmeal = setmealService.getById(id);
        mod.addAttribute("setmeal",setmeal);
        return "mobile_setmeal_detail";
    }*/
    /**
     * 用户端查询套餐详情
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result2 findById(Integer id){
        log.info("查询套餐详情：{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result2(true,"查询成功",setmeal);
    }
    /**
     * 用户端查询所有套餐
     * @return
     */
    @PostMapping("/getSetmeal")
    public Result2 getSetmeal(){
        log.info("查询所有套餐");
        List<Setmeal> list = setmealService.getAll();
        return new Result2(true,"查询成功",list);
    }
}
